package com.example.studentknowledgesys.controller;

import com.example.studentknowledgesys.dto.ArticleDetailDTO;
import com.example.studentknowledgesys.entity.Article;
import com.example.studentknowledgesys.service.ArticleService;
import com.example.studentknowledgesys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    // 创建文章
    @PostMapping("/add")
    public Result createArticle(@RequestBody ArticleRequest request) {
        System.out.println( "创建文章");
        ArticleResponse response = articleService.createArticle(request);
        return Result.success(response);
    }
    // 编辑文章
    @PutMapping("/edit")
    public Result updateArticle(@RequestBody Article article) {
        System.out.println("ll:"+article);
        Article updatedArticle = articleService.updateArticle(article);
        return updatedArticle != null ? Result.success(updatedArticle) : Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }
    // 更新文章状态
    @PutMapping("/status/{id}")
    public Result updateArticleStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = articleService.updateArticleStatus(id, status);
        return success ? Result.success() : Result.error(403,"更新文章状态失败");
    }
    // 删除文章
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Long id) {
        boolean deleted = articleService.deleteArticle(id);
        if (deleted) {
             return Result.success();
        }
         return Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }

    // 获取文章详情
    @GetMapping("/{id}")
    public Result getArticleById(@PathVariable Long id) {
        Article article = articleService.getArticleById(id);
        if (article != null) {
             return Result .success(article);
        }
        return Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }
    @GetMapping("/list")
    public List<ArticleDetailDTO> getArticleDetails(@RequestParam(required = false) String tagName, @RequestParam(required = false) Long authorId) {
        return articleService.getArticleDetails(tagName,authorId);
    }
    // 获取单个文章详情
    @GetMapping("/detail/{articleId}")
    public ArticleDetailDTO getArticleDetail(@PathVariable Long articleId) {
        return articleService.getArticleDetail(articleId);
    }

    // 获取所有文章
    @GetMapping
    public Result getAllArticles() {
        List<Article> articles = articleService.getAllArticles();
        return Result.success(articles);
    }

    // 根据标签获取文章
    @GetMapping("/by-tag/{tagId}")
    public Result getArticlesByTag(@PathVariable Long tagId) {
        List<Article> articles = articleService.getArticlesByTag(tagId);
        return Result.success(articles);
    }

    // 请求和响应对象
    public static class ArticleRequest {
        private String title;
        private String content;
        private Long tagId;
        private Long authorId;
        private boolean isDraft;

        // 省略getter/setter

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Long getTagId() {
            return tagId;
        }

        public void setTagId(Long tagId) {
            this.tagId = tagId;
        }

        public Long getAuthorId() {
            return authorId;
        }

        public void setAuthorId(Long authorId) {
            this.authorId = authorId;
        }

        public boolean isDraft() {
            return isDraft;
        }

        public void setDraft(boolean draft) {
            isDraft = draft;
        }
    }

    public static class ArticleResponse {
        private Article article;
        private int pointsAwarded;

        public Article getArticle() {
            return article;
        }

        public void setArticle(Article article) {
            this.article = article;
        }

        public int getPointsAwarded() {
            return pointsAwarded;
        }

        public void setPointsAwarded(int pointsAwarded) {
            this.pointsAwarded = pointsAwarded;
        }


    }

}
